﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using KharaSoft.ProjectManager.Core;
using System.Collections.ObjectModel;

namespace KharaSoft.ProjectManager.Windows
{
    public class ProjectListViewModel : INotifyPropertyChanged
    {
        private ICollection<Project> _availableProjects
            = new ObservableCollection<Project>(new List<Project>(new[]
                                                                      {
                                                                          new Project
                                                                              {
                                                                                  Name = "My First Project",
                                                                                  Tasks = new List<Task>(
                                                                                      new[]
                                                                                          {
                                                                                              new Task
                                                                                                  {
                                                                                                      Name =
                                                                                                          "My first Task"
                                                                                                  },
                                                                                              new Task
                                                                                                  {
                                                                                                      Name =
                                                                                                          "My second task"
                                                                                                  }
                                                                                          })

                                                                              },
                                                                          new Project
                                                                              {
                                                                                  Name = "A very important project"
                                                                              }
                                                                      }));

        private ICommand _AddProjectCommand;

        public ProjectListViewModel()
        {
            _AddProjectCommand = new DelegateCommand(
            null, o => AddProject());
            _eventAggregator = new EventAggregator();
        }


        public ICommand AddProjectCommand
        {
            get { return _AddProjectCommand; }
        }

        private  void AddProject()
        {
            _availableProjects.Add(new Project()
            {
                Name = "A new Project" + DateTime.Now.ToString()
            });
        }

        public ICollection<Project> AvailableProjects
        {
            get { return _availableProjects; }
            set
            {
                _availableProjects = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("AvailableProjects"));
                }
            }
        }

        private Project _selectedProject;
        private EventAggregator _eventAggregator;

        public Project SelectedProject
        {
            get { return _selectedProject; }
            set
            {
                _selectedProject = value;
                _eventAggregator.FireEvent(_selectedProject);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedProject"));
                }
            }
        }



        public event PropertyChangedEventHandler PropertyChanged;
    }
}
